package cn.xdd.dbutil;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.log4j.Logger;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

//完成连接的获取 以及  连接的关闭 两个方法
public class DbUtil {
	public static int Count = 1;

	private static  final Logger LOGGER = Logger.getLogger(DbUtil.class);




	public static final String DRIVER = "com.mysql.jdbc.Driver";
	public static final String URL = "jdbc:mysql://115.29.209.196:3306/bookmanage2";
	public static final String USERNAME = "root";
	public static final String PASSWORD = "Jiansheyin1";
	/*
	 * 创建连接池BasicDataSource
	 */
	public static BasicDataSource dataSource = new BasicDataSource();
	public static int size =1;
	//静态代码块
	static {
//对连接池对象 进行基本的配置
		dataSource.setDriverClassName(DRIVER); // 这是要连接的数据库的驱动
		dataSource.setUrl(URL); //指定要连接的数据库地址
		dataSource.setUsername(USERNAME); //指定要连接数据的用户名
		dataSource.setPassword(PASSWORD); //指定要连接数据的密码
		dataSource.setInitialSize(4);


	}

	/*
	 * 返回连接池对象
	 */
	public static DataSource getDataSource() {

		return dataSource;
	}
/*	private static  Connection cn;
	//获取连接对象
	public static synchronized   Connection getConn(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://115.29.209.196:3306/bookmanage2";
			cn = DriverManager.getConnection(url, "root", "Jiansheyin1");
			LOGGER.debug("    cn.toString():"+cn.toString());

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cn;
	}

	//关闭连接  关闭之前获得的连接
	public static void closeConn(){
		if(cn!=null){
			try {
				cn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}*/


//	锁getconnect会建立几次新连接
/*	private static Connection cn;
	    private static  final Logger LOGGER = Logger.getLogger(DbUtil.class);
	//获取连接对象
	public static synchronized Connection getConn(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://115.29.209.196:3306/bookmanage2";
			cn = DriverManager.getConnection(url, "root", "Jiansheyin1");
			 LOGGER.debug("    cn.toString():"+cn.toString());

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cn;
	}

	//关闭连接  关闭之前获得的连接
	public static void closeConn(){
		if(cn!=null){
			try {
				cn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}*/
}
